package com.chinasoft.obj;

import com.chinasoft.enumdef.Direction;
import com.chinasoft.enumdef.Elem;
import lombok.*;

@Setter
@Getter
@EqualsAndHashCode
public class Box implements CommonAble {
    int x;
    int y;
//    Elem[][] map;
    // 箱子缺省是 装的草地
    Elem contain = Elem.Glass;

    MapData data;

    @Override
    public String toString() {
        return "Box{" +
                "x=" + x +
                ", y=" + y +
                ", contain=" + contain +
                '}';
    }

    // 创建一个箱子， 使用有参构造方法
    public Box(int x, int y, MapData data) {
        this.x = x;
        this.y = y;
//        this.map = map;
        this.data = data;
    }

    @Override
    public Box getNextBox(Direction dr) {
        return data.getNextBox(this, dr);
    }

    @Override
    public boolean canMove(Direction dr) {
        Elem elem = getNextObj(dr);
        if (elem == Elem.Glass || elem == Elem.Gold) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断 2 个箱子对象是不是同一个对象
     * @param x
     * @param y
     * @return
     */
    public boolean match(int x, int y) {
        return true;
    }

    @Override
    public void move(Direction dr) {
//        map[y][x] = contain;
        data.setElem(y, x, contain);
        switch(dr) {
            case Right:
                x++;
//                contain = map[y][x];
                contain = data.getElem(y, x);
//                map[y][x] = contain == Elem.Gold ? Elem.BoxGold: Elem.Box;
                data.setElem(y, x, contain == Elem.Gold ? Elem.BoxGold: Elem.Box);
                break;
            case Left:
                x--;
                contain = data.getElem(y, x);
                data.setElem(y, x, contain == Elem.Gold ? Elem.BoxGold: Elem.Box);
                break;
            case Down:
                y++;
                contain = data.getElem(y, x);
                data.setElem(y, x, contain == Elem.Gold ? Elem.BoxGold: Elem.Box);
                break;
            case Up:
                y--;
                contain = data.getElem(y, x);
                data.setElem(y, x, contain == Elem.Gold ? Elem.BoxGold: Elem.Box);
                break;
            default:
                System.out.println("unkown move in box");
                break;
        }
    }

    @Override
    public Elem getNextObj(Direction dr) {
        return data.getNextCode(this, dr);
    }

    public boolean  isOk() {
        // 如果箱子里面 有金子， 返回  ok
        if (contain == Elem.Gold) {
            return true;
        }
        return false;
    }

}
